﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 **/

using System;
using System.ComponentModel;
using System.Windows.Input;
using Peter.Common;
using Peter.Common.MainMenu;
using Peter.Ftp.Data;

namespace Peter.Ftp.Logic
{
   /// <summary>
   /// Represents an ftp menu item.
   /// </summary>
   public class FtpConnectionMenuItem : PeterMainMenuItem
   {
      /// <summary>
      /// Initializes a new ftp menu item.
      /// </summary>
      /// <param name="connectionData">The menu item's connection data.</param>
      /// <param name="loadCommand">The command to load the connection item.</param>
      public FtpConnectionMenuItem (FtpConnectionData connectionData, ICommand loadCommand)
      {
         if (connectionData == null)
            throw new ArgumentNullException ("connectionData");

         this.Icon = MainMenuIcon.Ftpaccounts;
         this.Title = connectionData.Name;
         this.ToolTip = connectionData.UserName + "@" + connectionData.Server;
         this.Command = loadCommand;
         this.CommandParameter = connectionData;
         connectionData.PropertyChanged += this.OnPropertyChanged;
      }

      /// <summary>
      /// Occurs when a property in the connection data changes.
      /// </summary>
      /// <param name="sender">FtpConnectionData</param>
      /// <param name="e">PropertyChangedEventArgs</param>
      private void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
      {
         var connectionData = (FtpConnectionData) sender;
         if (connectionData != null)
         {
            if (e.PropertyName == "Name")
            {
               this.Title = connectionData.Name;
            }
            else if (e.PropertyName == "UserName" || e.PropertyName == "Server")
            {
               this.ToolTip = connectionData.UserName + "@" + connectionData.Server;
            }
         }
      }
   }
}
